//
//  LoginView.m
//  Gunz
//
//  Created by Joggy on 15/12/15.
//  Copyright © 2015年 NUPT. All rights reserved.
//

#import "LoginView.h"

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self prepareForLayout];
    UITapGestureRecognizer *endEditTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditTap)];
    [self addGestureRecognizer:endEditTap];
    return self;
}

- (void)prepareForLayout {
    //背景
    _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [GZConst getScreenWidth], [GZConst getScreenHeight])];
    _bgImageView.image = [UIImage imageNamed:@"pattern (3) 拷贝"];
    [self addSubview:_bgImageView];
    //标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 44, 110)];
    _titleLabel.text = @"群智社";
    _titleLabel.numberOfLines = 1;
    _titleLabel.font = [UIFont systemFontOfSize:32];
    _titleLabel.textColor = [UIColor whiteColor];
    [_titleLabel sizeToFit];
    _titleLabel.layer.position = CGPointMake([GZConst getScreenWidth]/2, [GZConst getScreenHeight]/4);
    [self addSubview:_titleLabel];
    //用户名输入框
    _nameView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIView* nameLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textFieldHeight - 2, textFieldHeight)];
    nameLeftView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.6];
    nameLeftView.layer.cornerRadius = lgCornerRadius;
    [_nameView addSubview:nameLeftView];
    UIImage *nameImage = [UIImage imageNamed:@"lg_account"];
    _nameImageView = [[UIImageView alloc] initWithImage:nameImage];
    _nameImageView.frame = CGRectMake(0, 0, iconWidth, iconWidth*([nameImage size].height/[nameImage size].width));
    _nameImageView.layer.position = CGPointMake(nameLeftView.frame.size.width/2, nameLeftView.frame.size.height/2);
    [nameLeftView addSubview:_nameImageView];
    _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake([nameLeftView frame].size.width + 2, 0, 240 - nameLeftView.frame.size.width - 2, textFieldHeight)];
    _nameTextField.layer.cornerRadius = lgCornerRadius;
    _nameTextField.backgroundColor = [UIColor whiteColor];
    _nameTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, innerInsert, textFieldHeight)];
    _nameTextField.leftViewMode = UITextFieldViewModeAlways;
    _nameTextField.tag = 1;
    [_nameView addSubview:_nameTextField];
    _nameView.frame = CGRectMake(0, 0, [nameLeftView frame].size.width + 2 + _nameTextField.frame.size.width, textFieldHeight);
    _nameView.layer.position = CGPointMake(screenWidth/2, screenHeight/2 - innerInsert/2 - [_nameView frame].size.height/2);
    [self addSubview:_nameView];
    //密码输入框
    UIView* passLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textFieldHeight - 2, textFieldHeight)];
    passLeftView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.6];
    passLeftView.layer.cornerRadius = lgCornerRadius;
    UIImage *passwordImage = [UIImage imageNamed:@"lg_password"];
    _passwordImageView = [[UIImageView alloc] initWithImage:passwordImage];
    _passwordImageView.frame = CGRectMake(0, 0, iconWidth, iconWidth*([nameImage size].height/[nameImage size].width));
    _passwordImageView.layer.position = CGPointMake(passLeftView.frame.size.width/2, passLeftView.frame.size.width/2);
    _passwordImageView.layer.cornerRadius = 6;
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake([passLeftView frame].size.width + 2, 0, 240 - nameLeftView.frame.size.width - 2, textFieldHeight)];
    _passwordTextField.backgroundColor = [UIColor whiteColor];
    _passwordTextField.layer.cornerRadius = lgCornerRadius;
    _passwordTextField.secureTextEntry = true;
    _passwordTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, innerInsert, textFieldHeight)];
    _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    _passwordTextField.tag = 2;
    _passwordView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [passLeftView frame].size.width + 2 + _passwordTextField.frame.size.width, textFieldHeight)];
    _passwordView.layer.position = CGPointMake(screenWidth/2, screenHeight/2 + innerInsert/2 + [_passwordView frame].size.height/2);
    [passLeftView addSubview:_passwordImageView];
    [_passwordView addSubview:passLeftView];
    [_passwordView addSubview:_passwordTextField];
    [self addSubview:_passwordView];
    
    //选项按钮
    _rememberButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rememberButton.frame = CGRectMake(_passwordView.layer.position.x, _passwordView.frame.origin.y + _passwordView.frame.size.height + 4, 16, 16);
    _rememberButton.backgroundColor = [UIColor clearColor];
    _rememberButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _rememberButton.layer.borderWidth = 1;
    _rememberButton.layer.cornerRadius = 4;
    [_rememberButton setImage:[UIImage imageNamed:@"lg_select_but"] forState:UIControlStateSelected];
    [self addSubview:_rememberButton];
    UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(_rememberButton.frame.origin.x + _rememberButton.frame.size.width + 2, 0, 60, 14)];
    CGPoint poi = lab1.layer.position;
    poi.y = _rememberButton.layer.position.y;
    lab1.layer.position = poi;
    lab1.font = [UIFont systemFontOfSize:10];
    lab1.textColor = [UIColor whiteColor];
    lab1.text = @"记住密码";
    [self addSubview:lab1];
    _autoLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _autoLoginButton.frame = CGRectMake(_rememberButton.frame.origin.x + 62, _rememberButton.frame.origin.y, 16, 16);
    _autoLoginButton.backgroundColor = [UIColor clearColor];
    _autoLoginButton.layer.borderWidth = 1;
    _autoLoginButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _autoLoginButton.layer.cornerRadius = 4;
    [_autoLoginButton setImage:[UIImage imageNamed:@"lg_select_but"] forState:UIControlStateSelected];
    [self addSubview:_autoLoginButton];
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(_autoLoginButton.frame.origin.x + _autoLoginButton.frame.size.width + 2, 0, 60, 14)];
    poi = lab2.layer.position;
    poi.y = _autoLoginButton.layer.position.y;
    lab2.layer.position = poi;
    lab2.font = [UIFont systemFontOfSize:10];
    lab2.textColor = [UIColor whiteColor];
    lab2.text = @"自动登录";
    [self addSubview:lab2];
    
    //登陆按钮
    _loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _loginButton.frame = CGRectMake(0, 0, 240, 40);
    _loginButton.layer.position = CGPointMake([GZConst getScreenWidth]/2, [GZConst getScreenHeight]*3/4);
    _loginButton.layer.cornerRadius = lgCornerRadius;
    [_loginButton setBackgroundColor:[UIColor whiteColor]];
    NSMutableAttributedString *loginTitle = [[NSMutableAttributedString alloc] initWithString:@"登陆"];
    NSRange loginRange = {0, loginTitle.length};
    [loginTitle addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:7/255 green:114/255 blue:48/255 alpha:1] range:loginRange];
    [loginTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font17] range:loginRange];
    [_loginButton setAttributedTitle:loginTitle forState:UIControlStateNormal];
    _loginButton.alpha = 0.6;
    [self addSubview:_loginButton];
    //找回密码按键
    _findPWButton = [UIButton buttonWithType:UIButtonTypeSystem];
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:@"找回密码"];
    NSRange titleRange = {0, titleAttributed.length};
    [titleAttributed addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font12] range:titleRange];
    [titleAttributed addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [titleAttributed addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:titleRange];
    [_findPWButton setAttributedTitle:titleAttributed forState:UIControlStateNormal];
    _findPWButton.frame = CGRectMake(innerInsert*2.5, screenHeight - innerInsert*3, titleAttributed.size.width, titleAttributed.size.height);
    [self addSubview:_findPWButton];
    //注册新用户按键
    _registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    NSMutableAttributedString *registerTitle = [[NSMutableAttributedString alloc] initWithString:@"注册新用户"];
    NSRange registerRange = {0, registerTitle.length};
    [registerTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font12] range:registerRange];
    [registerTitle addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:registerRange];
    [registerTitle addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:registerRange];
    [_registerButton setAttributedTitle:registerTitle forState:UIControlStateNormal];
    _registerButton.frame = CGRectMake(screenWidth - registerTitle.size.width - innerInsert*2.5, screenHeight - innerInsert*3, registerTitle.size.width, registerTitle.size.height);
    [self addSubview:_registerButton];
    
    _noticeView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, screenWidth, 25)];
    _noticeView.backgroundColor = [UIColor clearColor];
    _noticeView.layer.masksToBounds = true;
    [self addSubview:_noticeView];
}

- (void)endEditTap {
    [self endEditing:true];
}

@end










